package com.ruoyi.buss.grade.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.ruoyi.buss.grade.domain.DataCustGradeCondition;
import com.ruoyi.buss.grade.domain.DataCustGradeInfoNew;
import com.ruoyi.buss.grade.mapper.DataCustGradeConditionMapper;
import com.ruoyi.buss.grade.mapper.DataCustGradeInfoNewMapper;
import com.ruoyi.buss.grade.service.IDataCustGradeInfoNewService;
import com.ruoyi.buss.system.domain.UserRole;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.buss.utils.GetRoleUtil;
import com.ruoyi.buss.utils.ListUtil;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 自定义查询结果(二期字段更新)Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class DataCustGradeInfoNewServiceImpl implements IDataCustGradeInfoNewService 
{
    @Autowired
    private DataCustGradeInfoNewMapper dataCustGradeInfoNewMapper;
    @Autowired
    private DataCustGradeConditionMapper dataCustGradeConditionMapper;


    public  List<DataCustGradeInfoNew> getResultNew(DataCustGradeInfoNew DataCustGradeInfoNew, LoginUser userInfo){
    	try {
			UserRole userRole = GetRoleUtil.GetRole(userInfo);
//			DataCustGradeInfoNew.setOrgId(userRole.getOrgId());
//			DataCustGradeInfoNew.setUserId(userRole.getUserId());
            DataCustGradeInfoNew.setOrgId(null);
            DataCustGradeInfoNew.setUserId(null);
		} catch (Exception e) {
			throw new CustomException(e.getMessage());
		}
    	if(StringUtils.isEmpty(DataCustGradeInfoNew.getPlanCode())){
            DataCustGradeInfoNew.setPlanCode("1");
        }
    	//查询方案
    	 List<DataCustGradeInfoNew> dataCustGradeInfoNew = dataCustGradeConditionMapper.queryConditionNEW(DataCustGradeInfoNew);
		return dataCustGradeInfoNew;
    }
    
    public List<DataCustGradeInfoNew> queryResultNew(DataCustGradeInfoNew DataCustGradeInfoNew,LoginUser loginUser) {
		
 
        List<DataCustGradeInfoNew> doList = new ArrayList<DataCustGradeInfoNew>();
         
    	try {
			UserRole userRole = GetRoleUtil.GetRole(loginUser);
//			DataCustGradeInfoNew.setOrgId(userRole.getOrgId());
//			DataCustGradeInfoNew.setUserId(userRole.getUserId());
            DataCustGradeInfoNew.setOrgId(null);
            DataCustGradeInfoNew.setUserId(null);

		} catch (Exception e) {
			throw new CustomException(e.getMessage());
		}
        doList = dataCustGradeInfoNewMapper.queryResult(DataCustGradeInfoNew);
        return doList;
	}
    
    public void addPlanNew(DataCustGradeCondition dataCustGradeInfoNew, LoginUser userInfo) {
		String userId = userInfo.getSysUser().getUserName();
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		dataCustGradeInfoNew.setOrgId(orgId);
		dataCustGradeInfoNew.setUserId(userId);
		dataCustGradeInfoNew.setCreateUser(userId);
		dataCustGradeInfoNew.setCreateTime(DateUtils.getNowDate());
        dataCustGradeInfoNew.setDelFlag("1");
        String planCode = GetOnlyId.getOnlyId();
        dataCustGradeInfoNew.setPlanCode(planCode);

        // 插入条件
        dataCustGradeConditionMapper.insertDataCustGradeCondition(dataCustGradeInfoNew);
	}
    
    
	public  List<DataCustGradeCondition> queryPlanNew(DataCustGradeCondition dataCustGradeInfoNewVo,LoginUser userInfo) {
		
		DataCustGradeCondition dataCustGradeInfoNew = new DataCustGradeCondition();
		String userId = userInfo.getSysUser().getUserName();
		 if (!(userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office"))) {
			 dataCustGradeInfoNew.setCreateUser(userId);
		 }
		dataCustGradeInfoNew.setDelFlag("1");
		dataCustGradeInfoNew.setPlanCode(dataCustGradeInfoNewVo.getPlanCode());
		dataCustGradeInfoNew.setPlanName(dataCustGradeInfoNewVo.getPlanName());
        List<DataCustGradeCondition> doList = dataCustGradeConditionMapper.selectDataCustGradeConditionList(dataCustGradeInfoNew);

        return doList;
	}
    
	public void deletePlanNew(List<DataCustGradeCondition> list) {
		for (DataCustGradeCondition vo : list)
        {	
			vo.setDelFlag("0");
			dataCustGradeConditionMapper.updateDataCustGradeCondition(vo);
        }
	}
    
    public List<DataCustGradeInfoNew> queryResultNews(DataCustGradeInfoNew dataCustGradeInfoNew) {
    	
    	List<DataCustGradeInfoNew> dataCustGradeInfo = dataCustGradeConditionMapper.queryConditionNEW(dataCustGradeInfoNew);
        List<DataCustGradeInfoNew> doList = new ArrayList<DataCustGradeInfoNew>();

//      LoginUser userInfo = new LoginUser();
        
//      try {
//        	UserRole userRole  = GetRoleUtil.GetRole(userInfo);
//        	dataCustGradeInfoNew.setOrgId(userRole.getOrgId());
//        	dataCustGradeInfoNew.setUserId(userRole.getUserId());
//		} catch (Exception e) {
//			throw new CustomException(e.getMessage());
//		}
	   	 DataCustGradeInfoNew DataCustGradeInfoNew = null;
	   	 if(!ListUtil.isNullOrEmpty(dataCustGradeInfo)) {
	   		 DataCustGradeInfoNew = dataCustGradeInfo.get(0);
	   	 }
		if (dataCustGradeInfo != null) 
			doList = dataCustGradeConditionMapper.queryResult(DataCustGradeInfoNew);
		else if("1".equals(dataCustGradeInfoNew.getGetFiles()) || dataCustGradeInfo == null)
			doList = dataCustGradeConditionMapper.queryResult(dataCustGradeInfoNew);
		
        return doList;
	}
    
    
    /**
     * 查询自定义查询结果(二期字段更新)
     * 
     * @param idCard 自定义查询结果(二期字段更新)ID
     * @return 自定义查询结果(二期字段更新)
     */
    @Override
    public DataCustGradeInfoNew selectDataCustGradeInfoNewById(String idCard)
    {
        return dataCustGradeInfoNewMapper.selectDataCustGradeInfoNewById(idCard);
    }

    /**
     * 查询自定义查询结果(二期字段更新)列表
     * 
     * @param dataCustGradeInfoNew 自定义查询结果(二期字段更新)
     * @return 自定义查询结果(二期字段更新)
     */
    @Override
    public List<DataCustGradeInfoNew> selectDataCustGradeInfoNewList(DataCustGradeInfoNew dataCustGradeInfoNew)
    {
        return dataCustGradeInfoNewMapper.selectDataCustGradeInfoNewList(dataCustGradeInfoNew);
    }

    /**
     * 新增自定义查询结果(二期字段更新)
     * 
     * @param dataCustGradeInfoNew 自定义查询结果(二期字段更新)
     * @return 结果
     */
    @Override
    public int insertDataCustGradeInfoNew(DataCustGradeInfoNew dataCustGradeInfoNew)
    {
        return dataCustGradeInfoNewMapper.insertDataCustGradeInfoNew(dataCustGradeInfoNew);
    }

    /**
     * 修改自定义查询结果(二期字段更新)
     * 
     * @param dataCustGradeInfoNew 自定义查询结果(二期字段更新)
     * @return 结果
     */
    @Override
    public int updateDataCustGradeInfoNew(DataCustGradeInfoNew dataCustGradeInfoNew)
    {
        return dataCustGradeInfoNewMapper.updateDataCustGradeInfoNew(dataCustGradeInfoNew);
    }

    /**
     * 批量删除自定义查询结果(二期字段更新)
     * 
     * @param idCards 需要删除的自定义查询结果(二期字段更新)ID
     * @return 结果
     */
    @Override
    public int deleteDataCustGradeInfoNewByIds(String[] idCards)
    {
        return dataCustGradeInfoNewMapper.deleteDataCustGradeInfoNewByIds(idCards);
    }

    /**
     * 删除自定义查询结果(二期字段更新)信息
     * 
     * @param idCard 自定义查询结果(二期字段更新)ID
     * @return 结果
     */
    @Override
    public int deleteDataCustGradeInfoNewById(String idCard)
    {
        return dataCustGradeInfoNewMapper.deleteDataCustGradeInfoNewById(idCard);
    }
}
